package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.UmsAdminMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.domain.HomeContentResult;
import com.macro.mall.portal.dto.SmsPopupAdvertiseDto;
import com.macro.mall.portal.service.HomeService;
import com.macro.mall.portal.service.SmsAdvertiseService;
import com.macro.mall.wx.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页内容管理Controller
 */
@Controller
@Api(tags = "HomeController", description = "首页内容管理")
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private HomeService homeService;
    @Autowired
    private SmsAdvertiseService advertiseService;
    @Autowired
    private UmsAdminMapper umsAdminMapper;
    @Autowired
    private AuthService authService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation("首页内容页信息展示")
    @RequestMapping(value = "/content", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<HomeContentResult> content(@RequestParam(value = "storeId") Long storeId, Double lon, Double lat) {
        HomeContentResult contentResult = this.homeService.content(storeId, lon, lat);
        return CommonResult.success(contentResult);
    }

    /**
     * 根据门店id获取当前门店的推荐商品
     *
     * @param storeId  门店id
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation("分页获取推荐商品")
    @RequestMapping(value = "/recommendProductList/{storeId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProduct>> recommendProductList(@PathVariable(value = "storeId") Long storeId,
                                                               @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsProduct> productList = homeService.recommendProductList(storeId, pageSize, pageNum);
        return CommonResult.success(productList);
    }

    @ApiOperation("获取首页商品分类")
    @RequestMapping(value = "/productCateList/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductCategory>> getProductCateList(@PathVariable Long parentId) {
        List<PmsProductCategory> productCategoryList = homeService.getProductCateList(parentId);
        return CommonResult.success(productCategoryList);
    }

    @ApiOperation("根据分类获取专题")
    @RequestMapping(value = "/subjectList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<CmsSubject>> getSubjectList(@RequestParam(required = false) Long cateId,
                                                         @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<CmsSubject> subjectList = homeService.getSubjectList(cateId, pageSize, pageNum);
        return CommonResult.success(subjectList);
    }

    @ApiOperation("分页获取人气推荐商品")
    @RequestMapping(value = "/hotProductList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsProduct>> hotProductList(@RequestParam long storeId,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        CommonPage<PmsProduct> productList = homeService.hotProductList(storeId, pageNum, pageSize);
        return CommonResult.success(productList);
    }

    @ApiOperation("分页获取新品推荐商品")
    @RequestMapping(value = "/newProductList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProduct>> newProductList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        List<PmsProduct> productList = homeService.newProductList(pageNum, pageSize);
        return CommonResult.success(productList);
    }

    /**
     * 根据门店广告id获取弹窗广告
     *
     * @param id 弹窗广告id
     * @return 商品id
     * @author 高强
     * @Date 2020年8月28日
     */
    @ApiOperation("根据门店广告id获取弹窗广告")
    @RequestMapping(value = "/getAdvertise", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SmsPopupAdvertiseDto> getAdvertise() {
        return advertiseService.getAdvertise();
    }

    /**
     * 根据门店id和商品id，获得商品信息
     *
     * @param storeId
     * @param userId
     * @return
     * @author 高强
     * @Date 2020年8月28日
     */
    @RequestMapping(value = "/getAdvertiseProduct", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProduct> getAdvertiseProduct(@RequestParam long storeId, @RequestParam long userId) {
        PmsProduct product = advertiseService.getProduct4Advertise(storeId, userId);
        return CommonResult.success(product);
    }

    /**
     * 根据门店Id获取当前门店的广告，作为轮播广告，广告数目在service层设为3
     *
     * @param storeId 门店id
     * @return 广告list
     * @author 高强
     * @Date 2020年8月28日
     */
    @RequestMapping(value = "/getCarouselAdvertise/{storeId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SmsHomeAdvertise>> getCarouselAdvertise(@PathVariable(value = "storeId") long storeId) {
        List<SmsHomeAdvertise> carouselAdvertiseList = advertiseService.getCarouselAdvertise(storeId);
        return CommonResult.success(carouselAdvertiseList);
    }

    /**
     * 根据门店Id获取当前门店的商品，作为轮播商品，商品数目在service层设为3
     *
     * @return 商品list
     * @author 高强
     * @Date 2020年8月29日
     */
    @RequestMapping(value = "/getCarouselPmsProduct", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProduct>> getCarouselProduct() {
        List<PmsProduct> carouselProduct = homeService.getCarouselProduct();
        return CommonResult.success(carouselProduct);
    }

    /**
     * 根据用户信息，判断当前用户是否绑定店铺（storeId是否为空），未绑定返回一定范围内多个店铺信息，绑定返回绑定的店铺信息
     *
     * @param member 用户信息
     * @param lat    纬度
     * @param lon    经度
     * @return 返回一定范围内多个店铺信息/绑定返回绑定的店铺信息
     */
    @RequestMapping(value = "/getStoreByUser", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<UmsStore>> getStoreByUser(@RequestBody UmsMember member,
                                                       @RequestParam Double lat,
                                                       @RequestParam Double lon) {
        List<UmsStore> storeByUser = homeService.getStoreByUser(member, lat, lon);
        return CommonResult.success(storeByUser);
    }

    /**
     * 首页显示绑定门店信息
     *
     * @return
     * @author 高强
     * @Date 2020年9月9日
     */
    @ResponseBody
    @RequestMapping(value = "getStoreInfo", method = RequestMethod.GET)
    public CommonResult<UmsStore> getStoreInfo() {
        return homeService.getStoreInfo();
    }

    /**
     * 后台管理用户绑定微信openId
     *
     * @param memberId
     * @param jsCode
     * @return
     * @author zhouboyu
     * @Date 2020年9月10日
     */
    @ApiOperation("获取微信登录openId")
    @RequestMapping(value = "/getOpenId", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public CommonResult<String> register(@RequestParam("memberId") Long memberId, @RequestParam("jsCode") String jsCode) {
        System.out.println("获取微信登录openId----memberId"+memberId);
        System.out.println("获取微信登录openId----jsCode"+jsCode);
        CommonResult<String> result;
        UmsAdmin umsAdmin = umsAdminMapper.selectByPrimaryKey(memberId);
        String openId = this.authService.getOpenId(jsCode);
        if (StringUtils.isNotBlank(openId)) {
            umsAdmin.setOpenId(openId);
            int i = this.umsAdminMapper.updateByPrimaryKey(umsAdmin);
            if (i > 0) {
                result = CommonResult.success(openId, "获取成功");
            } else {
                result = CommonResult.failed();
            }
        } else {
            result = CommonResult.failed();
        }
        return result;
    }

//    @ApiOperation("获取微信登录openId")
//    @RequestMapping(value = "/jie", method = RequestMethod.GET)
//    @ResponseBody
//    public CommonResult<String> jie() {
//        CommonResult<String> result;
//        String str = "$10$lwUeKhM96lTFlMzWxZQ1c.9f1/ksyCp4XqlAwnkM9IgziiIZ/0lGW";
//        passwordEncoder.encode();
//        return CommonResult.success(encode);
//    }
}
